//index.js
//获取应用实例
const app = getApp()
const api = require('../../../config/api.js');
var dateTimePicker = require('../../../util/dateTimePicker.js');

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
   
    toast1Hidden: true,
    modalHidden: true,
    notice_str: '',
    
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTimeArray2: null,
    dateTime1: null,
    dateTime2: null,
    startYear: 2018,
    endYear: 2999
  },
  
  onLoad: function () {
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    // 精确到分的处理，将数组的秒去掉
    var lastArray = obj1.dateTimeArray.pop();
    var lastTime = obj1.dateTime.pop();
    var lastArray = obj2.dateTimeArray.pop();
    var lastTime = obj2.dateTime.pop();
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime
    });
  },
  bindDateChange: function (e) {
    var that = this;
    that.setData({
      date: e.detail.value
    })
    console.log(that.data.date);
  },
  //提交信息
  formSubmit: function (e) {
    //表单校验
    if(!this.checkForm(e)){
        return false;
    }
    var that = this;
    var formData = e.detail.value;
    //提交表单
    wx.request({
      url: api.INFO,//提交路径
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) { 
        console.log(res.data.code)
        console.log(res.data.msg)
        if (res.data.code==200){
          wx.showToast({
            title: '预约成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }else{
          wx.showToast({
            title: '服务器忙，请稍后再试',
            icon: 'succes',
            duration: 2000,
            mask: true
          })
        }
      
        //清空表单
        that.setData({
          username: '',
          phone1:'',
          count:''
        })
      }
    })
  },

  // checkForm:function(e){
    
  //   if (e.detail.value.enterdate.length == 0) {
  //     wx.showToast({
  //       title: "请选择日期",
  //       icon: 'loading',
  //       duration: 1000,
  //       mask: true
  //     })
  //     return false;
  //   }
    
  //   return true;
  // },


//定义函数，修改时间用的 
  changeDateTime1(e) {
    this.setData({ dateTime1: e.detail.value });
  },
    changeDateTime2(e) {
    this.setData({ dateTime2: e.detail.value });
  },
  //修改开始时间
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },

    //修改结束时间
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },




  //小程序转发
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/page/index/index',
      success: function (res) {
        // 转发成功
        console.log("转发成功")
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败")
      }
    }
  }
})
